<?php
// 安全验证字符串，为空则不验证
$token = 'XXXX';

// 验证来源
if ($_SERVER['HTTP_USER_AGENT'] !== 'git-oschina-hook') {
    header('HTTP/1.1 403 Permission Denied');
    die('Permission denied.');
}

// 如果启用验证，并且验证失败，返回错误
$httpToken =$_SERVER['HTTP_X_GITEE_TOKEN'] ?? '';
if ($token && $httpToken != $token) {
    header('HTTP/1.1 403 Permission Denied');
    die('Permission denied.');
}

//如果请求体内容为空，返回错误
$payload = file_get_contents('php://input');
if (!$payload) {
    header('HTTP/1.1 400 Bad Request');
    die('HTTP HEADER or POST is missing.');
}

$payload = json_decode($payload);
// 本地仓库路径
$local = '/data/wwwroot/' . $payload->repository->name;

// 如果仓库目录不存在，返回错误
if (!is_dir($local)) {
    header('HTTP/1.1 500 Internal Server Error');
    die('Local directory is missing');
}

exec('cd ' . $local . '; git pull origin master');
die('success');
